<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="stomp.resources.html">资源类型</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.stomp.html">Stomp 函数</a></div>
 <div class="up"><a href="book.stomp.html">Stomp</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="stomp.examples" class="chapter">
 <h1>范例</h1>

 
  <p class="para">
   <div class="example" id="example-4534">
    <p><strong>Example #1 面向对象风格</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$queue&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'/queue/foo'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$msg&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;connection&nbsp;*/<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stomp&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Stomp</span><span style="color: #007700">(</span><span style="color: #DD0000">'tcp://localhost:61613'</span><span style="color: #007700">);<br />}&nbsp;catch(</span><span style="color: #0000BB">StompException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Connection&nbsp;failed:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;send&nbsp;a&nbsp;message&nbsp;to&nbsp;the&nbsp;queue&nbsp;'foo'&nbsp;*/<br /></span><span style="color: #0000BB">$stomp</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">send</span><span style="color: #007700">(</span><span style="color: #0000BB">$queue</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$msg</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;subscribe&nbsp;to&nbsp;messages&nbsp;from&nbsp;the&nbsp;queue&nbsp;'foo'&nbsp;*/<br /></span><span style="color: #0000BB">$stomp</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">subscribe</span><span style="color: #007700">(</span><span style="color: #0000BB">$queue</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;read&nbsp;a&nbsp;frame&nbsp;*/<br /></span><span style="color: #0000BB">$frame&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$stomp</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">readFrame</span><span style="color: #007700">();<br /><br />if&nbsp;(</span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">body&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$msg</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$frame</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;acknowledge&nbsp;that&nbsp;the&nbsp;frame&nbsp;was&nbsp;received&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stomp</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ack</span><span style="color: #007700">(</span><span style="color: #0000BB">$frame</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;close&nbsp;connection&nbsp;*/<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$stomp</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(StompFrame)#2 (3) {
  [&quot;command&quot;]=&gt;
  string(7) &quot;MESSAGE&quot;
  [&quot;headers&quot;]=&gt;
  array(5) {
    [&quot;message-id&quot;]=&gt;
    string(41) &quot;ID:php.net-55293-1257226743606-4:2:-1:1:1&quot;
    [&quot;destination&quot;]=&gt;
    string(10) &quot;/queue/foo&quot;
    [&quot;timestamp&quot;]=&gt;
    string(13) &quot;1257226805828&quot;
    [&quot;expires&quot;]=&gt;
    string(1) &quot;0&quot;
    [&quot;priority&quot;]=&gt;
    string(1) &quot;0&quot;
  }
  [&quot;body&quot;]=&gt;
  string(3) &quot;bar&quot;
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4535">
   <p><strong>Example #2 过程化风格</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$queue&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'/queue/foo'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$msg&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;connection&nbsp;*/<br /></span><span style="color: #0000BB">$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stomp_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'ssl://localhost:61612'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;check&nbsp;connection&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$link</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Connection&nbsp;failed:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">stomp_connect_error</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;begin&nbsp;a&nbsp;transaction&nbsp;*/<br /></span><span style="color: #0000BB">stomp_begin</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'t1'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;send&nbsp;a&nbsp;message&nbsp;to&nbsp;the&nbsp;queue&nbsp;'foo'&nbsp;*/<br /></span><span style="color: #0000BB">stomp_send</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$queue</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$msg</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">'transaction'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'t1'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">/*&nbsp;commit&nbsp;a&nbsp;transaction&nbsp;*/<br /></span><span style="color: #0000BB">stomp_commit</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'t1'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;subscribe&nbsp;to&nbsp;messages&nbsp;from&nbsp;the&nbsp;queue&nbsp;'foo'&nbsp;*/<br /></span><span style="color: #0000BB">stomp_subscribe</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$queue</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;read&nbsp;a&nbsp;frame&nbsp;*/<br /></span><span style="color: #0000BB">$frame&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stomp_read_frame</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$frame</span><span style="color: #007700">[</span><span style="color: #DD0000">'body'</span><span style="color: #007700">]&nbsp;===&nbsp;</span><span style="color: #0000BB">$msg</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$frame</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;acknowledge&nbsp;that&nbsp;the&nbsp;frame&nbsp;was&nbsp;received&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">stomp_ack</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$frame</span><span style="color: #007700">[</span><span style="color: #DD0000">'headers'</span><span style="color: #007700">][</span><span style="color: #DD0000">'message-id'</span><span style="color: #007700">]);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;close&nbsp;connection&nbsp;*/<br /></span><span style="color: #0000BB">stomp_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [&quot;command&quot;]=&gt;
  string(7) &quot;MESSAGE&quot;
  [&quot;body&quot;]=&gt;
  string(3) &quot;bar&quot;
  [&quot;headers&quot;]=&gt;
  array(6) {
    [&quot;transaction&quot;]=&gt;
    string(2) &quot;t1&quot;
    [&quot;message-id&quot;]=&gt;
    string(41) &quot;ID:php.net-55293-1257226743606-4:3:-1:1:1&quot;
    [&quot;destination&quot;]=&gt;
    string(10) &quot;/queue/foo&quot;
    [&quot;timestamp&quot;]=&gt;
    string(13) &quot;1257227037059&quot;
    [&quot;expires&quot;]=&gt;
    string(1) &quot;0&quot;
    [&quot;priority&quot;]=&gt;
    string(1) &quot;0&quot;
  }
}
</pre></div>
    </div>
   </div>
  </p>
 
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="stomp.resources.html">资源类型</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.stomp.html">Stomp 函数</a></div>
 <div class="up"><a href="book.stomp.html">Stomp</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
